package com.ooho.lesson05.mvc.controller;

import com.ooho.lesson05.mvc.model.SiteBean;
import com.ooho.lesson05.mvc.service.SiteService;
import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by hugohoo on 10/11/16.
 */
public class SitesController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        String URI = req.getRequestURI();
        System.out.println("URI: " + URI);
        if(URI.equals("/lesson05/")){
            index(req, resp);
        }else if(URI.equals("/lesson05/edit")){
            edit(req, resp);
        }else if(URI.equals("/lesson05/delete_do")){
            delete_do(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        String URI = req.getRequestURI();
        System.out.println("URI : " + URI);

        switch (URI) {
            case "/lesson05/add_do/":
                add_do(req, resp);
                break;
            case "/lesson05/edit_do/":
                edit_do(req, resp);
                break;
            case "/lesson05/delete_do/":
                delete_do(req, resp);
                break;
        }
    }

    private void delete_do(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("delete_do.");

        SiteBean sb = new SiteBean();
        sb.setId(Integer.parseInt(req.getParameter("id")));

        if(SiteService.delete(sb) == 1){
            resp.sendRedirect("/lesson05/");
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("edit..");

        SiteBean sb = SiteService.find(Integer.parseInt(req.getParameter("id")));
        req.setAttribute("site", sb);

        getServletContext().getRequestDispatcher("/lesson05/edit.jsp").forward(req, resp);
    }

    private void edit_do(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("edit_do.");

        SiteBean sb = new SiteBean();
        sb.setTitle(req.getParameter("title"));
        sb.setUrl(req.getParameter("url"));
        sb.setId(Integer.parseInt(req.getParameter("id")));

        if(SiteService.update(sb) == 1){
            resp.sendRedirect("/lesson05/");
        }
    }

    private void add_do(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // pack parameter to SiteBean
        SiteBean sb = new SiteBean();
        sb.setTitle(req.getParameter("title"));
        sb.setUrl(req.getParameter("url"));

        // use site service to do business logic
        int rslt = SiteService.insert(sb);

        // send redirect
        resp.sendRedirect("/lesson05/");
    }


    private void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<SiteBean> siteList = SiteService.find();
        req.setAttribute("siteList", siteList);
        getServletContext().getRequestDispatcher("/lesson05/index.jsp").forward(req, resp);
    }
}
